Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
driver_MMA845X.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*! \file driver_MMA845x.c
31  \brief Provides init() and read() functions for the MMA845x 3-axis accel family.
32 
33  Supports MMA8451Q, MMA8452Q and MMA8453Q. Key differences which are applicable
34  to this driver are shown in the table below.
35 
36  Feature | MMA8451Q | MMA8452Q | MMA8453Q
37  --------|----------|----------|---------
38  # Bits | 14 | 12 | 10
39  FIFO | 32-deep | NONE | NONE
40 
41  All three have the MSB of the result registers located in the same location,
42  so if we read as one 16-bit value, the only thing that should change g/count
43  will be which range (+/- 2/4/8g) we are on.
44 */
45 
46 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
47 #include "sensor_fusion.h" // Sensor fusion structures and types
48 #include "sensor_drv.h"
49 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
50 #include "MMA845x.h" // describes the MMA845x register definition and its bit mask
51 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
52 #define MMA845x_COUNTSPERG 8192.0
53 #define MMA8451_ACCEL_FIFO_SIZE 32
54 
55 #if F_USING_ACCEL
56 
57 // Command definition to read the WHO_AM_I value.
58 const registerreadlist_t MMA845x_WHO_AM_I_READ[] =
59 {
60  { .readFrom = MMA845x_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
61 };
62 
63 // Command definition to read the number of entries in the accel FIFO.
64 const registerreadlist_t MMA845x_F_STATUS_READ[] =
65 {
66  { .readFrom = MMA845x_STATUS, .numBytes = 1 }, __END_READ_DATA__
67 };
68 
69 // Command definition to read the number of entries in the accel FIFO.
70 registerreadlist_t MMA845x_DATA_READ[] =
71 {
72  { .readFrom = MMA845x_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
73 };
74 
75 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
76 const registerwritelist_t MMA8451_Initialization[] =
77 {
78  // OK: write 0000 0000 = 0x00 to CTRL_REG1 to place MMA845x into standby
79  // [7-1] = 0000 000
80  // [0]: active=0
81  { MMA845x_CTRL_REG1, 0x00, 0x00 },
82 
83  // Not applicable to the 8452/3.
84  // OK: write 0100 0000 = 0x40 to F_SETUP to enable FIFO in continuous (circular) mode
85  // [7-6]: F_MODE[1-0]=01 for FIFO continuous mode
86  // [5-0]: F_WMRK[5-0]=000000 for no FIFO watermark
87  { MMA845x_F_SETUP, 0x40, 0x00 },
88 
89  // OK: write 0000 0001 = 0x01 to XYZ_DATA_CFG register to set g range
90  // [7-5]: reserved=000
91  // [4]: HPF_OUT=0
92  // [3-2]: reserved=00
93  // [1-0]: FS=01 for +/-4g: 512 counts / g = 8192 counts / g after 4 bit left shift
94  { MMA845x_XYZ_DATA_CFG, 0x01, 0x00 },
95 
96  // OK: write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
97  // [7]: ST=0: self test disabled
98  // [6]: RST=0: reset disabled
99  // [5]: unused
100  // [4-3]: SMODS=00
101  // [2]: SLPE=0: auto sleep disabled
102  // [1-0]: mods=10 for high resolution (maximum over sampling)
103  { MMA845x_CTRL_REG2, 0x02, 0x00 },
104 
105  // write 00XX X001 to CTRL_REG1 to set data rate and exit standby
106  // [7-6]: aslp_rate=00
107  // [5-3]: dr=111 for 1.56Hz data rate giving 0x39
108  // [5-3]: dr=110 for 6.25Hz data rate giving 0x31
109  // [5-3]: dr=101 for 12.5Hz data rate giving 0x29
110  // [5-3]: dr=100 for 50Hz data rate giving 0x21
111  // [5-3]: dr=011 for 100Hz data rate giving 0x19
112  // [5-3]: dr=010 for 200Hz data rate giving 0x11
113  // [5-3]: dr=001 for 400Hz data rate giving 0x09
114  // [5-3]: dr=000 for 800Hz data rate giving 0x01
115  // [2]: unused=0
116  // [1]: f_read=0 for normal 16 bit reads
117  // [0]: active=1 to take the part out of standby and enable sampling
118 #if (ACCEL_ODR_HZ <= 1)
119  { MMA845x_CTRL_REG1, 0x39, 0x00 }, // select 1.5625Hz ODR,
120 #elif (ACCEL_ODR_HZ <= 6)
121  { MMA845x_CTRL_REG1, 0x31, 0x00 }, // select 6.25Hz ODR
122 #elif (ACCEL_ODR_HZ <= 12)
123  { MMA845x_CTRL_REG1, 0x29, 0x00 }, // select 12.5Hz ODR
124 #elif (ACCEL_ODR_HZ <= 50)
125  { MMA845x_CTRL_REG1, 0x21, 0x00 }, // select 50Hz ODR
126 #elif (ACCEL_ODR_HZ <= 100)
127  { MMA845x_CTRL_REG1, 0x19, 0x00 }, // select 100Hz ODR
128 #elif (ACCEL_ODR_HZ <= 200)
129  { MMA845x_CTRL_REG1, 0x11, 0x00 }, // select 200Hz ODR
130 #elif (ACCEL_ODR_HZ <= 400)
131  { MMA845x_CTRL_REG1, 0x09, 0x00 }, // select 400Hz ODR
132 #else
133  { MMA845x_CTRL_REG1, 0x01, 0x00 }, // select 800Hz ODR
134 #endif
135  __END_WRITE_DATA__
136 };
137 
138 // MMA845x_Initialization is exactly the same as the above, but without
139 // the FIFO initialization.
140 const registerwritelist_t MMA845x_Initialization[] =
141 {
142  { MMA845x_CTRL_REG1, 0x00, 0x00 },
143  { MMA845x_XYZ_DATA_CFG, 0x01, 0x00 },
144  { MMA845x_CTRL_REG2, 0x02, 0x00 },
145 #if (ACCEL_ODR_HZ <= 1)
146  { MMA845x_CTRL_REG1, 0x39, 0x00 }, // select 1.5625Hz ODR,
147 #elif (ACCEL_ODR_HZ <= 6)
148  { MMA845x_CTRL_REG1, 0x31, 0x00 }, // select 6.25Hz ODR
149 #elif (ACCEL_ODR_HZ <= 12)
150  { MMA845x_CTRL_REG1, 0x29, 0x00 }, // select 12.5Hz ODR
151 #elif (ACCEL_ODR_HZ <= 50)
152  { MMA845x_CTRL_REG1, 0x21, 0x00 }, // select 50Hz ODR
153 #elif (ACCEL_ODR_HZ <= 100)
154  { MMA845x_CTRL_REG1, 0x19, 0x00 }, // select 100Hz ODR
155 #elif (ACCEL_ODR_HZ <= 200)
156  { MMA845x_CTRL_REG1, 0x11, 0x00 }, // select 200Hz ODR
157 #elif (ACCEL_ODR_HZ <= 400)
158  { MMA845x_CTRL_REG1, 0x09, 0x00 }, // select 400Hz ODR
159 #else
160  { MMA845x_CTRL_REG1, 0x01, 0x00 }, // select 800Hz ODR
161 #endif
162  __END_WRITE_DATA__
163 };
164 
165 // All sensor drivers and initialization functions have the same prototype.
166 // sfg is a pointer to the master "global" sensor fusion structure.
167 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
168 
169 // sfg = pointer to top level (generally global) data structure for sensor fusion
170 int8_t MMA845x_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
171 {
172  int32_t status;
173  uint8_t reg;
174  status = Register_I2C_Read(sensor->bus_driver, sensor->addr, MMA845x_WHO_AM_I, 1, &reg);
175  if (status==SENSOR_ERROR_NONE) {
176  sfg->Accel.iWhoAmI = reg;
177  switch (reg) {
178  case MMA8451_WHO_AM_I_WHOAMI_VALUE:
179  case MMA8452_WHO_AM_I_WHOAMI_VALUE:
180  case MMA8453_WHO_AM_I_WHOAMI_VALUE: break;
181  default: return(SENSOR_ERROR_INIT);
182  }
183  } else {
184  return(status);
185  }
186 
187  // Configure and start the MMA845x sensor. This does multiple register writes
188  // (see MMA845x_Initialization definition above)
189  if (reg==MMA8451_WHO_AM_I_WHOAMI_VALUE) {
190  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, MMA8451_Initialization );
191  } else {
192  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, MMA845x_Initialization );
193  }
194 
195  // Stash some needed constants in the SF data structure for this sensor
196  sfg->Accel.iCountsPerg = MMA845x_COUNTSPERG;
197  sfg->Accel.fgPerCount = 1.0F / MMA845x_COUNTSPERG;
198  sfg->Accel.fgPerCount = 1.0F / MMA845x_COUNTSPERG;
199 
200  sensor->isInitialized = F_USING_ACCEL;
201  sfg->Accel.isEnabled = true;
202 
203  return (status);
204 }
205 
206 int8_t MMA845x_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
207 {
208  uint8_t I2C_Buffer[6 * MMA8451_ACCEL_FIFO_SIZE]; // I2C read buffer
209  int8_t status; // I2C transaction status
210  int8_t j; // scratch
211  uint8_t sensor_fifo_count = 1;
212  int16_t sample[3];
213 
214  if(sensor->isInitialized != F_USING_ACCEL)
215  {
216  return SENSOR_ERROR_INIT;
217  }
218 
219  // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
220  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, MMA845x_F_STATUS_READ, I2C_Buffer );
221  if (status==SENSOR_ERROR_NONE) {
222  if (sfg->Accel.iWhoAmI==MMA8451_WHO_AM_I_WHOAMI_VALUE) {
223  sensor_fifo_count = I2C_Buffer[0] & 0x3F;
224  } else {
225  sensor_fifo_count = (I2C_Buffer[0] & 0x08)>>3;
226  }
227  // return if there are no measurements in the sensor FIFO.
228  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
229  if (sensor_fifo_count == 0) return SENSOR_ERROR_READ;
230  } else {
231  return(status);
232  }
233 
234  MMA845x_DATA_READ[0].readFrom = MMA845x_OUT_X_MSB;
235  MMA845x_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
236  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, MMA845x_DATA_READ, I2C_Buffer );
237 
238  if (status==SENSOR_ERROR_NONE) {
239  for (j = 0; j < sensor_fifo_count; j++)
240  {
241  sample[CHX] = (I2C_Buffer[6 * j ] << 8) | I2C_Buffer[6 * j + 1];
242  sample[CHY] = (I2C_Buffer[6 * j + 2] << 8) | I2C_Buffer[6 * j + 3];
243  sample[CHZ] = (I2C_Buffer[6 * j + 4] << 8) | I2C_Buffer[6 * j + 5];
244  conditionSample(sample); // truncate negative values to -32767
245  addToFifo((FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
246  }
247  }
248 
249  return (status);
250 }
251 
252 
253 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
254 const registerwritelist_t MMA845x_IDLE[] =
255 {
256  // Set ACTIVE = other bits unchanged
257  { MMA845x_CTRL_REG1, 0x00, 0x01 },
258  __END_WRITE_DATA__
259 };
260 
261 // MMA845x_Idle places the sensor into Standby mode (wakeup time = 2/ODR+1ms)
262 int8_t MMA845x_Idle(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
263 {
264  int32_t status;
265  if(sensor->isInitialized == F_USING_ACCEL) {
266  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, MMA845x_IDLE );
267  sensor->isInitialized = 0;
268  sfg->Accel.isEnabled = false;
269  } else {
270  return SENSOR_ERROR_INIT;
271  }
272  return status;
273 }
274 #endif // F_USING_ACCEL
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:77
#define MMA845x_COUNTSPERG
#define MMA8451_ACCEL_FIFO_SIZE
void addToFifo(FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The top level fusion structure.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
The sensor_fusion.h file implements the top level programming interface.
#define CHZ
Provides function prototypes for driver level interfaces.
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:76
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint16_t addr
I2C address if applicable.